<template>
  <div class="my-tag">
    <input 
    v-if="isEdit" 
    class="input" 
    type="text"
     placeholder="输入标签" 
     :value="value"
     ref="input" 
     @blur="close"
     @keyup.enter="$event.target.blur()"
     
     />
    <div class="text" @dblclick="showInput">{{ value }}</div>
  </div>
</template>

<script>
export default {
  props:{
    value: {
      type:String,
      required: true,
    },
  },
  data() {
    return {
      // 默认不在编辑状态
      isEdit: false,
    };
  },
  methods: {
    showInput() {
      this.isEdit = true;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    close(e) {
     if (e.target.value.trim() === "") {
      alert("标签不能为空");
     }else{
      this.$emit("input",e.target.value);
     }
     this.isEdit = false;
     console.log(123);
    },
  },
};
</script>

<style scoped lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
